<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<object classid="clsid:5EB842AE-5C49-4FD8-8CE9-77D4AF9FD4FF" id="IdrControl1" width="0" height="0" >
		<%--如果需要控件图片不显示，请把上面的width和height改为0即可--%>
	</object>

	<title>读者添加</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			jQuery.validator.addMethod("isMobile", function(value, element) {
				var length = value.length;
				var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
				return this.optional(element) || (length == 11 && mobile.test(value));
			}, "请正确填写您的手机号码");

			jQuery.validator.addMethod("isIdCardNo", function (value, element) {
				return this.optional(element) || isIdCardNo(value);
			}, "请正确输入您的身份证号码");
			//增加身份证验证

			function isIdCardNo(num) {
				var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
				var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
				var varArray = new Array();
				var intValue;
				var lngProduct = 0;
				var intCheckDigit;
				var intStrLen = num.length;
				var idNumber = num;
				// initialize
				if ((intStrLen != 15) && (intStrLen != 18)) {
					return false;
				}
				// check and set value
				for (i = 0; i < intStrLen; i++) {
					varArray[i] = idNumber.charAt(i);
					if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
						return false;
					} else if (i < 17) {
						varArray[i] = varArray[i] * factorArr[i];
					}
				}
				if (intStrLen == 18) {
					//check date
					var date8 = idNumber.substring(6, 14);
					if (isDate8(date8) == false) {
						return false;
					}
					// calculate the sum of the products
					for (i = 0; i < 17; i++) {
						lngProduct = lngProduct + varArray[i];
					}
					// calculate the check digit
					intCheckDigit = parityBit[lngProduct % 11];
					// check last digit
					if (varArray[17] != intCheckDigit) {
						return false;
					}
				}
				else {        //length is 15
					//check date
					var date6 = idNumber.substring(6, 12);
					if (isDate6(date6) == false) {
						return false;
					}
				}
				return true;
			}
			function isDate6(sDate) {
				if (!/^[0-9]{6}$/.test(sDate)) {
					return false;
				}
				var year, month, day;
				year = sDate.substring(0, 4);
				month = sDate.substring(4, 6);
				if (year < 1700 || year > 2500) return false
				if (month < 1 || month > 12) return false
				return true
			}

			function isDate8(sDate) {
				if (!/^[0-9]{8}$/.test(sDate)) {
					return false;
				}
				var year, month, day;
				year = sDate.substring(0, 4);
				month = sDate.substring(4, 6);
				day = sDate.substring(6, 8);
				var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
				if (year < 1700 || year > 2500) return false
				if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29;
				if (month < 1 || month > 12) return false
				if (day < 1 || day > iaMonthDays[month - 1]) return false
				return true
			}

			$("#btnOther").click(function(){
				$("#otherinfo").toggle(1000);
			});


			$("#no").focus();
			$("#inputForm").validate({
				rules: {
					loginName: {remote: "${ctx}/sys/user/checkLoginName?oldLoginName=" + encodeURIComponent('${user.loginName}')}
				},
				messages: {
					loginName: {remote: "用户登录名已存在"},
					confirmNewPassword: {equalTo: "输入与上面相同的密码"}
				},
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});

            /**
             * 点击复选框时判断是否选中,选中时复制身份证号
             */
            $("#use_checkbox").click(function () {
                if ($(this).is(':checked')) {
                    var rdIdk = $("#rdIdk").val();
                    $("#rdCardno").val(rdIdk);
                } else {
                    $("#rdCardno").val("");
                }
            });
		});
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/book/reader/list">读者列表</a></li>
		<li class="active"><a href="${ctx}/book/reader/form?id=${reader.id}">读者${not empty reader.id?'修改':'添加'}</a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="reader" action="${ctx}/book/reader/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>

		<div class="control-group">
			<label class="control-label">读者编号:</label>
			<div class="controls">
				<form:input path="rdId" htmlEscape="false" maxlength="50"  readonly="true" />
				<span class="help-inline"><font color="red">系统自动生成</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">借书证号:</label>
			<div class="controls">
				<form:input path="rdCardno" htmlEscape="false" maxlength="50"  readonly="false" class="required"/>
                <input id="use_checkbox" type="checkbox">使用身份证号
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">一卡通卡号:</label>
			<div class="controls">
				<form:input path="rdIccardno" htmlEscape="false" maxlength="50"  readonly="false"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">读者姓名:</label>
			<div class="controls">
				<form:input path="rdName" htmlEscape="false" maxlength="50" class="required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>


		<div class="control-group">
			<label class="control-label">读者科室/年级:</label>
			<div class="controls">
				<form:select path="rdGrad" class="input-medium"  htmlEscape="false" maxlength="50" >
					<form:options items="${fns:getDictList('rd_Grad')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
			</div>
			<%--<label class="control-label">读者科室/年纪:</label>--%>
			<%--<div class="controls">--%>
				<%--<form:input path="rdGrad" htmlEscape="false" maxlength="50" />--%>
			<%--</div>--%>
		</div>
		<div class="control-group">
			<label class="control-label">身份证号:</label>
			<div class="controls">
				<form:input path="rdIdk" htmlEscape="false" maxlength="18" class="isIdCardNo"/>
				&nbsp;&nbsp;<input type="button" value="读身份证" onClick="Idcard();">
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">联系电话:</label>
			<div class="controls">
				<form:input path="rdTele" htmlEscape="false" maxlength="11" class="digits isMobile"/>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">读者性别:</label>
			<div class="controls">
				<form:select id="rdSex" path="rdSex" class="input-medium">
					<form:options items="${fns:getDictList('sex')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">注册日期:</label>
			<div class="controls">
				<input id="rdReg" name="rdReg" type="text" readonly="readonly" maxlength="20" class="input-small Wdate"
					   value="<fmt:formatDate value="${reader.rdReg}" pattern="yyyy-MM-dd"/>" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">读者状态:</label>
			<div class="controls">
				<form:select path="rdState" class="input-medium">
					<form:options items="${fns:getDictList('rdstatus')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">读者角色:</label>
			<div class="controls">
				<form:select path="rdRoleId" id="rdRoleId"  class="input-medium required">
					<c:forEach items="${page.list}" var="roler">
						<form:option value="${roler.rdRoleId}">
						${roler.roleName}
						</form:option>
					</c:forEach>
				</form:select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">有效期限:</label>
			<div class="controls">
			<input id="rdOver" name="rdOver" type="text" readonly="readonly" maxlength="20" class="input-small Wdate required"
				   value="<fmt:formatDate value="${reader.rdOver}" pattern="yyyy-MM-dd"/>"
				   onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>
				<span class="help-inline"><font color="red">*</font> </span>
				</div>
		</div>

		<%--<div class="control-group">--%>
			<%--<label class="control-label">充值余额:</label>--%>
			<%--<div class="controls">--%>
				<%--<form:input path="rdMny" htmlEscape="false" maxlength="50" />--%>
			<%--</div>--%>
		<%--</div>--%>
		<div class="control-group all">
			<input id="btnOther" class="btn btn-info" type="button" value="其他信息" />
		</div>

		<div id="otherinfo" style="display: none;">
		<div class="control-group">
			<label class="control-label">出生日期:</label>
			<div class="controls">
				<input id="rdBirth" name="rdBirth" type="text" readonly="readonly" maxlength="20" class="input-small Wdate"
					   value="<fmt:formatDate value="${reader.rdBirth}" pattern="yyyy-MM-dd"/>" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">工作单位:</label>
			<div class="controls">
				<form:input path="rdCorp" htmlEscape="false" maxlength="50" />
			</div>
		</div>


		<div class="control-group">
			<label class="control-label">读者部门:</label>
			<div class="controls">
				<form:input path="rdDpt" htmlEscape="false" maxlength="50" />
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">读者职务:</label>
			<div class="controls">
				<form:input path="rdOcc" htmlEscape="false" maxlength="50" />
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">联系地址:</label>
			<div class="controls">
				<form:input path="rdAdr" htmlEscape="false" maxlength="50" />
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">邮政编码:</label>
			<div class="controls">
				<form:input path="rdPst" htmlEscape="false" maxlength="50" />
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">电子邮件:</label>
			<div class="controls">
				<form:input path="rdEml" htmlEscape="false" maxlength="50" class="email"/>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">备注信息:</label>
			<div class="controls">
				<form:textarea path="reMarks" htmlEscape="false" rows="3" maxlength="200" class="input-xlarge"/>
			</div>
		</div>
		</div>
		<div class="form-actions">
			<input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>

	</form:form>
	<script language="javascript">
		var PortOpened = 0;
		var CpuCardFound = 0;

		function Idcard() {
            try {
                var ax = new ActiveXObject("IDRCONTROL.IdrControlCtrl.1");
            } catch (e) {
                alertx("控件未安装");
            }
            IdrControl1.RepeatRead(1);   //设置是否重复读卡  0-不重复  1-重复
			IdrControl1.setMute(0);   //设置是否静音读卡  0-不静音  1-静音

			//注意：第一个参数为对应的设备端口，USB型为1001，串口型为1至16
            var result = IdrControl1.ReadCard("1001", "");
            if (result === 1) {
                var readerName = IdrControl1.GetName();
                var idcardNo = IdrControl1.GetCode();
                $("#rdName").val(readerName);
                $("#rdIdk").val(idcardNo);
                // 如果选中复选框,需要给借书证号赋值
                $("#rdCardno").val(idcardNo);

                var sexStr = parseInt(idcardNo.substr(16, 1));
                
                if(sexStr%2 === 0){
                    $("#rdSex").val("2").select2();
                }else{
                    $("#rdSex").val("1").select2();
                }
                $("#rdBirth").val(IdrControl1.GetBirthYear() + "-" + IdrControl1.GetBirthMonth() + "-" + IdrControl1.GetBirthDay());
            } else {
                if (result === -1) {
                    alertx("端口初始化失败！");
                }
                if (result === -2) {
                    alertx("请将身份证放到读卡器上！");
                }
                if (result === -3) {
                    alertx("读取数据失败！");
                }
                if (result === -4) {
                    alertx("生成照片文件失败，请检查设定路径和磁盘空间！");
                }
            }
		}

		function ICcard() {
			//注意：参数为对应的设备端口，iDR210为8159，iDR200 USB型为1001，iDR200串口型为1至16
            var result = IdrControl1.ReadICCard("8159");
			result = IdrControl1.RepeatRead(1);   //设置是否重复读卡  0-不重复  1-重复
			result = IdrControl1.setMute(0);   //设置是否静音读卡  0-不静音  1-静音

			if (result == 1) {
                $("#rdIccardno").val(IdrControl1.GetICCardSN());
			} else {
				if (result == -1)
                    alertx("端口初始化失败！");
				if (result == -2)
                    alertx("读IC卡失败");
			}
		}

		function FindCPUCard() {
			document.all.oTable.rows(14).cells(1).innerText = "";
			PortOpened = IdrControl1.InitComm(1001);
			CpuCardFound = 0;
			if (PortOpened != 1) {

				IdrControl1.CloseComm();
                alertx("端口初始化失败！");
				PortOpened = 0;
				return;
			}

			CpuCardFound = IdrControl1.FindICCard();
			document.all.oTable.rows(14).cells(1).innerText = "找到卡类型：" + CpuCardFound;

			if (CpuCardFound0 == 0) {
				IdrControl1.CloseComm();
                alertx("未找到卡！");
				CpuCardFound = 0;
				return;
			}
			if (CpuCardFound0 > 3) {
				IdrControl1.CloseComm();
                alertx("找卡失败！");
				CpuCardFound = 0;
				return;
			}
		}

		function ReadSDCard() {
			document.all.oTable.rows(16).cells(1).innerText = "";
			document.all.oTable.rows(17).cells(1).innerText = "";
			document.all.oTable.rows(18).cells(1).innerText = "";
			document.all.oTable.rows(19).cells(1).innerText = "";
			document.all.oTable.rows(20).cells(1).innerText = "";

			if (CpuCardFound != 2) {
                alertx("请先找CPU卡！");
				CpuCardFound = 0;
				return;
			}

			result = IdrControl1.ReadCitizenCard();

			if (result == 1) {
				document.all.oTable.rows(16).cells(1).innerText = IdrControl1.GetCityCardNO();
				document.all.oTable.rows(17).cells(1).innerText = IdrControl1.GetCityCardName();
				document.all.oTable.rows(18).cells(1).innerText = IdrControl1.GetCityCardIDType();
				document.all.oTable.rows(19).cells(1).innerText = IdrControl1.GetCityCardIDCode();
				document.all.oTable.rows(20).cells(1).innerText = IdrControl1.GetCityCardAppInfo();
			} else if (result == -1)
				alertx("端口初始化失败！");
			if (result == -2)
				alertx("找CPU卡失败！");
			if (result == -3)
				alertx("读市民卡失败！");
		}

		function APDU_CMD() {
			if (CpuCardFound != 2) {
				alertx("请先找CPU卡！");
				CpuCardFound = 0;
				return;
			}

			document.all.oTable.rows(22).cells(1).innerText = IdrControl1.Routon_APDU(document.form1.apdustr.value);
		}
	</script>
</body>
</html>